package scparser.factory.sceneFactory;

import java.io.BufferedReader; 
import java.io.IOException;

import lib.common.Constants;
import model.AA;
import model.Image;
import model.Scene;
import scparser.ParserHelper;
import scparser.factory.primitiveFactory.FactoryException;

public class SceneImageFactory extends SceneFactory {
	private int width;
	private int height;
	private AA aa = new AA();

	public SceneImageFactory(Scene scene) {
		super.scene = scene;
	}

	@Override
	public Object create(BufferedReader reader) throws FactoryException {
		String line;
		try {
			while ((line = reader.readLine()) != null) {
				line = line.trim();
				if (line.equals(Constants.S_CLOSE)) {
					break;
				} else if (line.contains("resolution")) {
					String[] values = ParserHelper.takeString(line, "resolution").trim().split(" ");
					width = Integer.parseInt(values[0]);
					height = Integer.parseInt(values[1]);
				} else if (line.contains("aa")) {
					String[] values = ParserHelper.takeString(line, "aa").trim().split(" ");
					aa.firstValue = Integer.parseInt(values[0]);
					aa.secondValue = Integer.parseInt(values[1]);
					
					if (aa.secondValue < 0) {
						throw new FactoryException("anti-aliasing value error");
					}
					
				}
			}
		} catch (IOException e) {
			throw new FactoryException(e.getMessage());
		} catch ( NumberFormatException e) {
			throw new FactoryException(e.getMessage());
		}
		
		Image ret = new Image();
		ret.aa = aa;
		ret.width = width;
		ret.height = height;
		
		return ret;
	}

}
